package com.fhc.laser_monitor_sw_android_rtsp_app.views;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

//目标锁定截图选定框
public class DrawView2 extends View {

    private Paint myPaint1;

    private float centerX = 0f;
    private float centerY = 0f;

    //外边框长度一半
    private final float halfRectangleLength = 100;
    private float scale = 1.5f;
    private float left;
    private float top;
    private float right;
    private float bottom;
    //十字架长度
    private final float lineLength = 20;


    public DrawView2(Context context) {
        this(context, null);
    }

    public DrawView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        myPaint1 = new Paint();
        myPaint1.setColor(Color.CYAN);// set color is red
        myPaint1.setStrokeWidth(2f);
        myPaint1.setStyle(Paint.Style.STROKE);//set style is stoke

        SharedPreferences sharedPreferences = context.getSharedPreferences("CALIBRATE",
                Activity.MODE_PRIVATE);

        this.centerX = sharedPreferences.getFloat("coordinateX0", 360f) * scale;
        this.centerY = sharedPreferences.getFloat("coordinateY0", 240f) * scale;

//        Log.e("TAG", "DrawView2: --------------  " + centerX);
//        Log.e("TAG", "DrawView2: --------------  " + centerY);
//        Log.e("TAG", "DrawView2: -----------564---  " + scale);

        float rectangleScaleLength = halfRectangleLength * scale;

        left = this.centerX - rectangleScaleLength;
        top = this.centerY - rectangleScaleLength;
        right = this.centerX + rectangleScaleLength;
        bottom = this.centerY + rectangleScaleLength;
    }

    public void drawRecForCalibration(float centerX, float centerY) {
        this.centerX = centerX;
        this.centerY = centerY;

        float rectangleScaleLength = halfRectangleLength * scale;

        left = this.centerX - rectangleScaleLength;
        top = this.centerY - rectangleScaleLength;
        right = this.centerX + rectangleScaleLength;
        bottom = this.centerY + rectangleScaleLength;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        myPaint1.setStrokeWidth(6f);
//        myPaint2.setStrokeWidth(6f);

        //绘制矩形
        canvas.drawRect(left, top, right, bottom, myPaint1);// square

//        canvas.drawText("X 轴起点：" + (int) left + "        Y 轴起点：" + (int) top,
//                left,
//                top - 20,
//                myPaint2);
//
//        canvas.drawText("X 轴终点：" + (int) right + "        Y 轴终点：" + (int) bottom,
//                left,
//                bottom + 35,
//                myPaint2);

//        Log.e("TAG", "DrawView2: --对比结果----: " + "XXX 轴起点：" + (int) left + "    XXX 轴终点：" + (int) right);
//        Log.e("TAG", "DrawView2: --对比结果----: " + "--Y 轴起点：" + (int) top +  "    --Y 轴终点：" + (int) bottom);

        //画十字
        //横线
        canvas.drawLine(centerX - lineLength, centerY, centerX + lineLength, centerY, myPaint1);
        //竖线
        canvas.drawLine(centerX, centerY - lineLength, centerX, centerY + lineLength, myPaint1);
    }

}
